package com.haifeng.lyw.jinganghc.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.haifeng.lyw.jinganghc.R;
import com.haifeng.lyw.jinganghc.bluetooth.SiriListItem;

import java.util.ArrayList;

public class ChatListAdapter extends BaseAdapter {

    private ArrayList<SiriListItem> list;
    private LayoutInflater mInflater;
    public boolean flage = false;

    public ChatListAdapter(Context context, ArrayList<SiriListItem> list2) {
        list = list2;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return list.size();
    }

    public Object getItem(int position) {
        return list.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public int getItemViewType(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final SiriListItem item = list.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder(
                    (View) convertView.findViewById(R.id.list_child),
                    (TextView) convertView.findViewById(R.id.chat_msg),
                    (ImageView) convertView.findViewById(R.id.chat_img),
                    (CheckBox) convertView.findViewById(R.id.checkbox_operate_data)
            );

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (item != null) {
            // 根据isSelected来设置checkbox的显示状况
            if (flage) {
                viewHolder.checkboxOperateData.setVisibility(View.VISIBLE);
            } else {
                viewHolder.checkboxOperateData.setVisibility(View.GONE);
            }

            viewHolder.checkboxOperateData.setChecked(item.isCheck);
            //注意这里设置的不是onCheckedChangListener，还是值得思考一下的
            viewHolder.checkboxOperateData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (item.isCheck) {
                        item.isCheck = false;
                    } else {
                        item.isCheck = true;
                    }
                }
            });
        }
        if (item.isSiri) {
            viewHolder.child.setBackgroundResource(R.mipmap.top_dk);
        } else {
            viewHolder.child.setBackgroundResource(R.mipmap.top_dk);
        }
        viewHolder.msg.setText(item.message);

        return convertView;
    }

    class ViewHolder {
        protected View child;
        protected TextView msg;
        protected ImageView img;
        protected CheckBox checkboxOperateData;

        public ViewHolder(View child, TextView msg, ImageView img, CheckBox checkboxOperateData) {
            this.child = child;
            this.msg = msg;
            this.img = img;
            this.checkboxOperateData = checkboxOperateData;
        }
    }

}
